package com.wwz;

import com.alibaba.excel.util.StringUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.wwz.demo.ContractExtVO;
import com.wwz.demo.ContractListParamRequest;
import com.wwz.demo.JacksonUtil;
import okhttp3.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * 合同数据远程导出
 * 1.远程调用雪山合同查询接口
 * 2.将获得的json文件导出存盘发给产品
 *
 * @author sailor
 * @date 2021-07-02
 **/
//public class ContractRemoteExport {
//    public static void main(String[] args) throws IOException {
//        OkHttpClient client = new OkHttpClient.Builder()
//                .connectTimeout(500, TimeUnit.SECONDS)
//                .readTimeout(500, TimeUnit.SECONDS)
//                .writeTimeout(500, TimeUnit.SECONDS)
//                .retryOnConnectionFailure(false)
//                .build();
//
//        ContractListParamRequest contractListParamRequest = new ContractListParamRequest();
//        contractListParamRequest.setStatusList(Collections.singletonList(4));
//        RequestBody requestBody = RequestBody.create(MediaType.parse(org.springframework.http.MediaType.APPLICATION_JSON_VALUE), JacksonUtil.obj2String(contractListParamRequest));
//
//
//        Request request = new Request.Builder()
//                .url("http://rc-xueshan.snowballfinance.com/brand/contract/list.json")
//                .post(requestBody)
//                .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36")
//                .addHeader("Cookie", "experimentation_subject_id=eyJfcmFpbHMiOnsibWVzc2FnZSI6IkltWm1NREk0T0RJNExXTTFaakF0TkRVeE9TMWhNV0l3TFdaa05qRTVNV0UzT1ROaVpDST0iLCJleHAiOm51bGwsInB1ciI6ImNvb2tpZS5leHBlcmltZW50YXRpb25fc3ViamVjdF9pZCJ9fQ%3D%3D--c3d7e4966f110a23af272c28379867d7d63c3f2b; _ga=GA1.2.2035764853.1614675635; _hjTLDTest=1; _hjid=570ab073-d792-4295-a148-329ed2eae7d3; _gid=GA1.2.778245455.1625189720; JSESSIONID=535f8fe1-d5fb-430b-9bb4-533aa8a525e0; username=sailor")
//                .addHeader("Content-Type", "application/json;charset=UTF-8")
//                .build();
//
//        Response response = client.newCall(request).execute();
//        if (!response.isSuccessful()) {
//            throw new IOException("Unexpected code " + response);
//        }
//
//        assert response.body() != null;
//        String respStr = response.body().string();
//        JsonObject departmentJsonObj = JsonParser.parseString(respStr).getAsJsonObject();
//        JsonArray asJsonArray = departmentJsonObj.getAsJsonObject("data").getAsJsonArray("list");
//
//        ArrayList<ContractExtVO> contractExtVOS = JacksonUtil.string2Obj(asJsonArray.toString(), new TypeReference<ArrayList<ContractExtVO>>() {
//        });
//        System.out.println(contractExtVOS.size());
//
//
//        HSSFWorkbook wb = new HSSFWorkbook();
//        HSSFSheet sheet = wb.createSheet("contract data");
//        //创建行
//        HSSFRow row_0 = sheet.createRow(0);
//
//        String[] tableHeader = new String[]{
//                "合同ID",
//                "客户ID",
//                "客户名称",
//                "签约公司",
//                "合同金额(元)",
//                "合同类型",
//                "合同状态",
//                "合同开始时间",
//                "合同结束时间",
//                "是否补合同",
//                "补合同耗时(天)",
//                "是否已归档",
//                "纸质合同编号",
//                "签约销售",
//                "所属销售",
//                "创建时间"};
//
//
//        for (int i = 0; i < tableHeader.length; i++) {
//            String header = tableHeader[i];
//            HSSFCell cell = row_0.createCell(i);
//            cell.setCellValue(header);
//        }
//
//
//        for (int i = 0; i < contractExtVOS.size(); i++) {
//            HSSFRow row = sheet.createRow(i + 1);
//            ContractExtVO contractExtVO = contractExtVOS.get(i);
//            HSSFCell cell_0 = row.createCell(0);
//            HSSFCell cell_1 = row.createCell(1);
//            HSSFCell cell_2 = row.createCell(2);
//            HSSFCell cell_3 = row.createCell(3);
//            HSSFCell cell_4 = row.createCell(4);
//            HSSFCell cell_5 = row.createCell(5);
//            HSSFCell cell_6 = row.createCell(6);
//            HSSFCell cell_7 = row.createCell(7);
//            HSSFCell cell_8 = row.createCell(8);
//            HSSFCell cell_9 = row.createCell(9);
//            HSSFCell cell_10 = row.createCell(10);
//            HSSFCell cell_11 = row.createCell(11);
//            HSSFCell cell_12 = row.createCell(12);
//            HSSFCell cell_13 = row.createCell(13);
//            HSSFCell cell_14 = row.createCell(14);
//            HSSFCell cell_15 = row.createCell(15);
//
//            cell_0.setCellValue(contractExtVO.getId());
//            cell_1.setCellValue(contractExtVO.getCustomerIds().get(0));
//            cell_2.setCellValue(String.join(SeparateConstant.COMMA, contractExtVO.getCustomerNames()));
//            cell_3.setCellValue(String.join(SeparateConstant.COMMA, contractExtVO.getSignCompanies()));
//            cell_4.setCellValue(contractExtVO.getAmount() != null ? contractExtVO.getAmount().toString() : StringUtils.EMPTY);
//            cell_5.setCellValue(ContractTypeEnum.getEnumByContractType(contractExtVO.getContractType()).getMessage());
//            cell_6.setCellValue(ContractStatus.getInstance(contractExtVO.getStatus()).getMessage());
//            cell_7.setCellValue(contractExtVO.getStartDate());
//            cell_8.setCellValue(contractExtVO.getEndDate());
//            cell_9.setCellValue(ContractSupplyStatusEnum.getInstance(contractExtVO.getSupplyStatus()).getMessage());
//            cell_10.setCellValue(contractExtVO.getSupplyTime());
//            cell_11.setCellValue(contractExtVO.getIsFiled());
//            cell_12.setCellValue(contractExtVO.getOfflineContractId());
//            cell_13.setCellValue(contractExtVO.getSignSale());
//            cell_14.setCellValue(contractExtVO.getBelongSale());
//            cell_15.setCellValue(JodaDateUtil.dateToStr(contractExtVO.getCreateTime(), DateFormatterConstant.YYMMDDHHMMSS));
//
//
//        }
//
//
//        try {
//            FileOutputStream output = new FileOutputStream("/Users/sailor/test.xls");
//            wb.write(output);
//            output.flush();
//            output.close();
//            System.out.println("成功创建excel文件");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//    }
//
//}



